﻿Partial Public Class App
    Inherits Application

    Private Shared _InitParams As Dictionary(Of String, String)
    Public Shared ReadOnly Property InitParams As Dictionary(Of String, String)
        Get
            Return _InitParams
        End Get
    End Property

    Public Shared ReadOnly Property IpAddress As String
        Get
            If InitParams.ContainsKey("IpAddress") Then
                Return InitParams("IpAddress")
            Else
                Return ""
            End If
        End Get
    End Property

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
        _InitParams = e.InitParams
        Me.RootVisual = New MainPage()
    End Sub

    Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs) Handles Me.UnhandledException
        If TypeOf e.ExceptionObject Is FormatException Then
            'No en fem res
        ElseIf e.ExceptionObject.StackTrace IsNot Nothing AndAlso e.ExceptionObject.StackTrace.Contains("SaveFileStream") Then
            'Quan obres un fitxer obert peta una estona més tard amb el garbage collector. Incontrolable.
            e.Handled = True
        Else

            Deployment.Current.Dispatcher.BeginInvoke(New Action(Of String)(AddressOf ReportError), e.ExceptionObject.Message & vbNewLine & e.ExceptionObject.StackTrace) '"Se ha producido un error accediendo al servidor. Revise su conexión a internet")
            If e.ExceptionObject.InnerException IsNot Nothing Then Deployment.Current.Dispatcher.BeginInvoke(New Action(Of String)(AddressOf ReportError), e.ExceptionObject.InnerException.Message)
            e.Handled = True
        End If
    End Sub

#Region "Mostrar Missatges"
    Public Shared Sub ReportError(ByVal Missatge As String)
        DirectCast(DirectCast(App.Current, App).RootVisual, MainPage).ShowStatusBar(MainPage.EnumTipusMissatge.Error, Missatge)
    End Sub

    Public Shared Sub ShowInfo(ByVal Missatge As String)
        DirectCast(DirectCast(App.Current, App).RootVisual, MainPage).ShowStatusBar(MainPage.EnumTipusMissatge.Informacio, Missatge)
    End Sub
    Public Shared Sub ShowWarning(ByVal Missatge As String)
        DirectCast(DirectCast(App.Current, App).RootVisual, MainPage).ShowStatusBar(MainPage.EnumTipusMissatge.Warning, Missatge)
    End Sub

    Public Shared Sub ShowMessage(ByVal Message As String)
        Dim child As InfoWindow = New InfoWindow(Message)
        child.Show()
    End Sub

    Public Shared Sub ShowMessage(ByVal e As Exception)
        ShowMessage(e.Message)
    End Sub
#End Region

    Public Sub Navigate(ByVal Uri As Uri)
        Try
            DirectCast(Me.RootVisual, MainPage).Navigate(Uri)
        Catch

        End Try
    End Sub


End Class
